package com.yequan.hbb.adapter;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yequan.hbb.R;
import com.yequan.hbb.app.MyApplication;
import com.yequan.hbb.customview.ConerImageView;
import com.yequan.hbb.customview.DateCount;
import com.yequan.hbb.entries.LearnHP;
import com.yequan.hbb.utils.CommonAdapter;
import com.yequan.hbb.utils.CornerImageUtils;
import com.yequan.hbb.utils.ImageUtils;
import com.yequan.hbb.utils.ShufflingAdapter;
import com.yequan.hbb.view.fragment.LearnHBFragment;
import com.yequan.hbb.view.fragment.lhp.SearchFragment;
import com.yequan.hbb.view.fragment.lhp.TypeFragment;
import com.yequan.hbb.view.fragment.lhp.WebViewFragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/10/13.
 */

public class LearnAdapter extends CommonAdapter<Object, LearnAdapter.LAViewHolder> {


    private String TYPE_SHUFFING = "轮播图";
    private Context context;
    public  static String TEXT_SEARCH = "搜索食谱、材料、达人";

    private String TEXT_TITLE_2 = "拼团";
    private String TEXT_TITLE_1 = "神器部落";
    private String TEXT_TITLE_3 = "精选菜单";
    private String TEXT_TITLE_4 = "热门榜单";
    private String TEXT_TITLE_5 = "食谱达人";
    private String TEXT_TITLE_6 = "分类食谱";
    private String TEXT_TITLE_7 = "日常活动";
    private String TEXT_TITLE_8 = "大课堂";
    private String TEXT_TITLE_9 = "友情链接";

    private int FLAG=2 << 24 + 1;
    private String text_1;
    private String  ALL_TYPE = "查看全部分类";
    private String  joinPeople = "%d人参与";

    public LearnAdapter(Context context) {
        super(context, new int[]{R.layout.wl_l_hp_header,
                R.layout.wl_l_hp_shuffing,
                R.layout.wl_l_hp_2,
                R.layout.wl_l_hp_3_3,
                R.layout.wl_l_hp_4,
                R.layout.wl_l_hp_5,
                R.layout.wl_l_hp_6,
                R.layout.wl_l_hp_1,//分类食谱
                R.layout.wl_l_hp_7,//日常活动
                R.layout.wl_l_hp_9,//大课堂
                R.layout.wl_l_hp_10//友情链接
        });
        this.context = context;
    }


    @Override
    public int getItemViewType(int position) {


        Object item = getItem(position);
        if (item instanceof LearnHP.DataBean.CategoryBean) {
            LearnHP.DataBean.CategoryBean categoryBean = (LearnHP.DataBean.CategoryBean) item;
            //LearnHP.DataBean.CategoryBean.ItemBean itemBean = categoryBean.getItem().get(position);
            if (TextUtils.equals(categoryBean.getTitle(), TYPE_SHUFFING)) {
                return 1;
            } else if (TextUtils.equals(categoryBean.getTitle(), TEXT_TITLE_1)) {
                return 2;
            } else if (TextUtils.equals(categoryBean.getTitle(), TEXT_TITLE_2)) {
                return 3;
            } else if (TextUtils.equals(categoryBean.getTitle(), TEXT_TITLE_3)) {
                return 4;
            } else if (TextUtils.equals(categoryBean.getTitle(), TEXT_TITLE_4)) {
                return 5;
            }else if (TextUtils.equals(categoryBean.getTitle(), TEXT_TITLE_5)) {
                return 6;
            }else if (TextUtils.equals(categoryBean.getTitle(), TEXT_TITLE_6)) {
                return 7;
            }else if (TextUtils.equals(categoryBean.getTitle(), TEXT_TITLE_7)) {
                return 8;
            }else if (TextUtils.equals(categoryBean.getTitle(), TEXT_TITLE_8)) {
                return 9;
            }else if (TextUtils.equals(categoryBean.getTitle(), TEXT_TITLE_9)) {
                return 10;
            }

        } else {
            return 0;
        }
        return super.getItemViewType(position);
    }


    @Override
    public void onBindView(LAViewHolder holder, Object data) {

        if (data instanceof LearnHP.DataBean.CategoryBean) {
            LearnHP.DataBean.CategoryBean bean = (LearnHP.DataBean.CategoryBean) data;



            Object tag = holder.itemView.getTag(FLAG);
            if (tag!=null) {
                return ;
            }
            if (TextUtils.equals(bean.getTitle(), TYPE_SHUFFING)) {
                //轮播
                if (holder.mViewPager.getAdapter()!=null){
                    return ;
                }
                List<LearnHP.DataBean.CategoryBean.ItemBean> item = bean.getItem();
                shuffing(holder, item);
            } else if (TextUtils.equals(bean.getTitle(), TEXT_TITLE_1)) {

                holder.title_header.setText(bean.getTitle());
                contentFill_1(holder, bean);

            } else if (TextUtils.equals(bean.getTitle(), TEXT_TITLE_2)) {


                holder.title_header.setText(bean.getTitle());
                holder.right_more.setVisibility(View.VISIBLE);
                groupFill(holder, bean);

            } else if (TextUtils.equals(bean.getTitle(), TEXT_TITLE_3)) {

                holder.title_header.setText(bean.getTitle());
                holder.right_more.setVisibility(View.VISIBLE);
                fillHeader1(holder,bean);

            } else if (TextUtils.equals(bean.getTitle(), TEXT_TITLE_4)) {

                holder.title_header.setText(bean.getTitle());
                fillHeader2(holder,bean);
            }else if (TextUtils.equals(bean.getTitle(), TEXT_TITLE_5)) {
                if (holder.recyclerView.getAdapter()!=null)
                    return ;
                holder.title_header.setText(bean.getTitle());
                holder.right_more.setVisibility(View.VISIBLE);
                fillHeader3(holder,bean);
            }
            else if (TextUtils.equals(bean.getTitle(), TEXT_TITLE_6)) {

                holder.title_header.setText(bean.getTitle());
                fillHeader4(holder,bean);
            }else if (TextUtils.equals(bean.getTitle(), TEXT_TITLE_7)) {

                holder.title_header.setText(bean.getTitle());
                holder.right_more.setVisibility(View.VISIBLE);
                fillHeader5(holder,bean);

            }else if (TextUtils.equals(bean.getTitle(), TEXT_TITLE_8)) {
                holder.title_header.setText(bean.getTitle());
                holder.right_more.setVisibility(View.VISIBLE);
                fillHeader6(holder,bean);
            }else if (TextUtils.equals(bean.getTitle(), TEXT_TITLE_9)) {
                holder.title_header.setText(bean.getTitle());
                fillHeader7(holder,bean);
            }

        } else {
            holder.searchTx.setText(TEXT_SEARCH);
            holder.searchTx.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    SearchFragment instance = SearchFragment.getInstance(null,-1);
                    FragmentTransaction transaction = ((AppCompatActivity) v.getContext())
                            .getSupportFragmentManager()
                            .beginTransaction()
                            .addToBackStack("SEARCH")
                          // .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
                            .setCustomAnimations(R.anim.fragment_in,R.anim.fragment_out);
                            transaction.replace(R.id.activity_main,instance).commit();
                    MyApplication.main.findViewById(R.id.main_rg).setVisibility(View.GONE);
                    MyApplication.main.findViewById(R.id.rl).setVisibility(View.GONE);
                }
            });
        }
        holder.itemView.setTag(FLAG,true);


    }

    private void fillHeader7(LAViewHolder holder, LearnHP.DataBean.CategoryBean bean) {
        List<LearnHP.DataBean.CategoryBean.ItemBean> item = bean.getItem();
        ImageUtils.loadImage(holder.iv_content_footer_1, item.get(0).getImage(),R.mipmap.default_error);
        ImageUtils.loadImage(holder.iv_content_footer_2, item.get(1).getImage(),R.mipmap.default_error);
        ImageUtils.loadImage(holder.iv_content_footer_3, item.get(2).getImage(),R.mipmap.default_error);
        ImageUtils.loadImage(holder.iv_content_footer_4, item.get(3).getImage(),R.mipmap.default_error);

    }

    private void fillHeader6(LAViewHolder holder, LearnHP.DataBean.CategoryBean bean) {

        List<LearnHP.DataBean.CategoryBean.ItemBean> item = bean.getItem();
        holder.iv_content_1.setCircle(true);
        holder.iv_content_2.setCircle(true);
        CornerImageUtils.loadImage(holder.iv_content_1, item.get(0).getImage(),R.mipmap.default_error);
        holder.tv_right_1.setText(item.get(0).getTitle());
        CornerImageUtils.loadImage(holder.iv_content_2, item.get(1).getImage(),R.mipmap.default_error);
        holder.tv_right_2.setText(item.get(1).getTitle());
    }

    private void fillHeader5(LAViewHolder holder, LearnHP.DataBean.CategoryBean bean) {

        List<LearnHP.DataBean.CategoryBean.ItemBean> item = bean.getItem();
        holder.tv_left_1.setText( item.get(0).getTitle());
        holder.tv_right_1.setText( String.format(joinPeople,item.get(0).getJoinNum()));
        holder.tv_left_2.setText( item.get(1).getTitle());
        holder.tv_right_3.setText( String.format(joinPeople,item.get(1).getJoinNum()));
        holder.tv_left_3.setText( item.get(2).getTitle());
        holder.tv_right_3.setText( String.format(joinPeople,item.get(2).getJoinNum()));
    }

    private void fillHeader4(LAViewHolder holder, LearnHP.DataBean.CategoryBean bean) {

        List<LearnHP.DataBean.CategoryBean.ItemBean> item = bean.getItem();
        holder.tv1.setText(item.get(0).getTitle());
        holder.tv2.setText(item.get(1).getTitle());
        holder.tv3.setText(item.get(2).getTitle());
        holder.tv4.setText(item.get(3).getTitle());
        holder.tv5.setText(item.get(4).getTitle());
        holder.tv6.setText(item.get(5).getTitle());
        holder.tv1.setOnClickListener(item.get(0));
        holder.tv2.setOnClickListener(item.get(1));
        holder.tv3.setOnClickListener(item.get(2));
        holder.tv4.setOnClickListener(item.get(3));
        holder.tv5.setOnClickListener(item.get(4));
        holder.tv6.setOnClickListener(item.get(5));
        holder.tv7.setText(ALL_TYPE);
        holder.tv7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TypeFragment instance = TypeFragment.instance();
                ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction()
                        .addToBackStack("TYPE_ALL")
                        .setCustomAnimations(R.anim.fragment_r_l,R.anim.fragment_l_r,R.anim.fragment_l_r_in,R.anim.fragment_r_l_out)
                        .add(R.id.main_container,instance)
                        .commit();
                MyApplication.main.findViewById(R.id.main_rg).setVisibility(View.GONE);
                ImageView iv=  ((ImageView) MyApplication.main.findViewById(R.id.rl).findViewById(R.id.main_message));
                        iv.setVisibility(View.VISIBLE);
                iv.setImageResource(R.mipmap.app_images_navigationbar_navigation_back);
            }
        });
    }

    private void fillHeader3(LAViewHolder holder, LearnHP.DataBean.CategoryBean bean) {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        holder.recyclerView.setLayoutManager(linearLayoutManager);
        holder.recyclerView.setAdapter(new UserRecyclerAdapter(context,bean.getItem()));

    }

    private void fillHeader2(LAViewHolder holder, LearnHP.DataBean.CategoryBean bean) {
        List<LearnHP.DataBean.CategoryBean.ItemBean> item = bean.getItem();
        CornerImageUtils.loadImage(holder.iv_content_1, bean.getItem().get(0).getImage(), 0);
        CornerImageUtils.loadImage(holder.iv_content_2, bean.getItem().get(1).getImage(), 0);
        CornerImageUtils.loadImage(holder.iv_content_3, bean.getItem().get(2).getImage(), 0);
        CornerImageUtils.loadImage(holder.iv_content_4, bean.getItem().get(3).getImage(), 0);
    }

    private void fillHeader1(LAViewHolder holder, LearnHP.DataBean.CategoryBean bean) {

        CornerImageUtils.loadImage(holder.iv_content, bean.getItem().get(0).getImage(), 0);


    }

    private void groupFill(LAViewHolder holder, LearnHP.DataBean.CategoryBean bean) {


        List<LearnHP.DataBean.CategoryBean.ItemBean> item = bean.getItem();

        for (LearnHP.DataBean.CategoryBean.ItemBean itemBean : item) {
            final View inflate = LayoutInflater.from(context).inflate(R.layout.wl_l_hp_3, holder.footerToolBars, false);
            if (itemBean.isIsSuccessGroupBuy()) {
                ImageView shuiyin_src = ((ImageView) inflate.findViewById(R.id.shuiyin_src));
                shuiyin_src.setVisibility(View.VISIBLE);
            }
            ImageView iv_content = ((ImageView) inflate.findViewById(R.id.iv_content));
            ImageUtils.loadImage(iv_content, itemBean.getImage(), R.mipmap.default_error);
            TextView footer_shuffing = (TextView) inflate.findViewById(R.id.footer_shuffing);
            text_1 = "已有%d人拼团";
            footer_shuffing.setText(String.format(text_1, itemBean.getGroupNum()));
            DateCount dateCount = ((DateCount) inflate.findViewById(R.id.date_count));

            String end = itemBean.getGroupBuyEndTime();
            String start = itemBean.getGroupBuyBeginTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
              //  Date dateS = sdf.parse(start);
                Date dateS=new Date();
                Date dateE = sdf.parse(end);

                long count = dateE.getTime() - dateS.getTime();
                if (count<=0) {
                    dateCount.setText("已结束拼团");
                }else {
                    dateCount.initTime(count);
                    dateCount.setFormat("%(s)");
                    //dateCount.
                    dateCount.setOnTimeCompleteListener(new DateCount.OnTimeCompleteListener() {
                        @Override
                        public void onTimeComplete() {
                            inflate.findViewById(R.id.shuiyin_src).setVisibility(View.INVISIBLE);
                        }
                    });
                    dateCount.start();
                }
                holder.footerToolBars.addView(inflate);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

    }

    private void contentFill_1(LAViewHolder holder, LearnHP.DataBean.CategoryBean bean) {

        final LearnHP.DataBean.CategoryBean.ItemBean itemBean = bean.getItem().get(0);
        CornerImageUtils.loadImage(holder.iv_content, itemBean.getImage(), 0);
        holder.iv_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                WebViewFragment instance = WebViewFragment.instance(itemBean.getTitle(),itemBean.getLink());
                FragmentTransaction transaction = ((AppCompatActivity) v.getContext())
                        .getSupportFragmentManager()
                        .beginTransaction()
                        .addToBackStack("SEARCH")
                        // .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
                        .setCustomAnimations(R.anim.fragment_in,R.anim.fragment_out);
                transaction.replace(R.id.main_container,instance).commit();
                MyApplication.main.findViewById(R.id.main_rg).setVisibility(View.GONE);
                MyApplication.main.findViewById(R.id.rl).setVisibility(View.GONE);
            }
        });


    }

    private void shuffing(final LAViewHolder holder, final List<LearnHP.DataBean.CategoryBean.ItemBean> itemBeanList) {


        for (LearnHP.DataBean.CategoryBean.ItemBean entry : itemBeanList) {
            entry.setImageUrl(entry.getImage());
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(R.drawable.point);
            int v = (int) context.getResources().getDimension(R.dimen.point);
            imageView.setPadding(v, v, v, v);
            holder.footerToolBars.addView(imageView);
        }
        ShufflingAdapter<LearnHP.DataBean.CategoryBean.ItemBean> adapter = new ShufflingAdapter<>(context, itemBeanList, R.mipmap.default_error);
        holder.mViewPager.setAdapter(adapter);
        holder.mViewPager.addOnPageChangeListener(holder);
        holder.mViewPager.setTag(itemBeanList);
        holder.mViewPager.setCurrentItem(1);
        holder.iv.post(new Runnable() {
            @Override
            public void run() {
                ViewCompat.setTranslationX(holder.iv, holder.footerToolBars.getLeft());
            }
        });
        //holder.handler.obtainMessage(0, 1, 0).sendToTarget();
    }

    public static class LAViewHolder extends CommonAdapter.ViewHolder implements ViewPager.OnPageChangeListener {

        private final ViewPager mViewPager;
        private final ImageView iv;
        private final LinearLayout footerToolBars;
        private final TextView searchTx;
        private final TextView footer_shuffing;
        private final ConerImageView iv_content;
        private final TextView title_header;
        private final ImageView right_more;

        private final ConerImageView iv_content_1;
        private final ConerImageView iv_content_2;
        private final ConerImageView iv_content_3;
        private final ConerImageView iv_content_4;
        private final RecyclerView recyclerView;
        private final ImageView iv_content_footer_1;
        private final ImageView iv_content_footer_2;
        private final ImageView iv_content_footer_3;
        private final ImageView iv_content_footer_4;


        private TextView tv_left_1;
        private TextView tv_right_1;
        private TextView tv_left_2;
        private TextView tv_right_2;
        private TextView tv_left_3;
        private TextView tv_right_3;


        private final TextView tv1;
        private final TextView tv2;
        private final TextView tv3;
        private final TextView tv4;
        private final TextView tv5;
        private final TextView tv6;
        private final TextView tv7;

        private View itemView;
        public LAViewHolder(View itemView) {
            super(itemView);

            this.itemView=itemView;

            iv_content_footer_1 = ((ImageView) itemView.findViewById(R.id.iv_content_footer_1));
            iv_content_footer_2 = ((ImageView) itemView.findViewById(R.id.iv_content_footer_2));
            iv_content_footer_3 = ((ImageView) itemView.findViewById(R.id.iv_content_footer_3));
            iv_content_footer_4 = ((ImageView) itemView.findViewById(R.id.iv_content_footer_4));

            tv_left_1=((TextView) itemView.findViewById(R.id.tv_left_1));
            tv_right_1=((TextView) itemView.findViewById(R.id.tv_right_1));
            tv_left_2=((TextView) itemView.findViewById(R.id.tv_left_2));
            tv_right_2=((TextView) itemView.findViewById(R.id.tv_right_2));
            tv_left_3=((TextView) itemView.findViewById(R.id.tv_left_3));
            tv_right_3=((TextView) itemView.findViewById(R.id.tv_right_3));

            tv1 = ((TextView) itemView.findViewById(R.id.tv1));
            tv2 = ((TextView) itemView.findViewById(R.id.tv2));
            tv3 = ((TextView) itemView.findViewById(R.id.tv3));
            tv4 = ((TextView) itemView.findViewById(R.id.tv4));
            tv5 = ((TextView) itemView.findViewById(R.id.tv5));
            tv6 = ((TextView) itemView.findViewById(R.id.tv6));
            tv7 = ((TextView) itemView.findViewById(R.id.tv7));


            recyclerView = ((RecyclerView) itemView.findViewById(R.id.recyler_view));

            iv_content_1 = ((ConerImageView) itemView.findViewById(R.id.iv_content_1));
            iv_content_2 = ((ConerImageView) itemView.findViewById(R.id.iv_content_2));

            iv_content_3 = ((ConerImageView) itemView.findViewById(R.id.iv_content_3));
            iv_content_4 = ((ConerImageView) itemView.findViewById(R.id.iv_content_4));


            searchTx = ((TextView) itemView.findViewById(R.id.wl_tool_bar_search));

            mViewPager = ((ViewPager) itemView.findViewById(R.id.view_pager));

            footer_shuffing = ((TextView) itemView.findViewById(R.id.footer_shuffing));
            iv = ((ImageView) itemView.findViewById(R.id.footer_focus_point));
            footerToolBars = ((LinearLayout) itemView.findViewById(R.id.footer_tool_bar_container));

            iv_content = ((ConerImageView) itemView.findViewById(R.id.iv_content));

            title_header = ((TextView) itemView.findViewById(R.id.title_header));
            right_more = ((ImageView) itemView.findViewById(R.id.right_more));

        }


        private Handler handler = new Handler(new Handler.Callback() {
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case 0:
                        int arg1 = msg.arg1;
                        if (arg1 > mViewPager.getAdapter().getCount() - 2) {
                            arg1 = 1;
                        }
                        mViewPager.setCurrentItem(arg1, arg1 != 1);
                        Message message = handler.obtainMessage(0, ++arg1, 0);
                        handler.sendMessageDelayed(message, 2000);
                        break;
                }
                return true;
            }
        });

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


            float value = (position - 1 + positionOffset) * footerToolBars.getChildAt(0).getWidth() + footerToolBars.getLeft();
            value = Math.max(value, footerToolBars.getLeft());
            value = Math.min(value, footerToolBars.getRight() - iv.getWidth());
            ViewCompat.setTranslationX(iv, value);
        }

        @Override
        public void onPageSelected(int position) {
            PagerAdapter adapter = mViewPager.getAdapter();
            if (position == 0) {
                position = adapter.getCount() - 2;
                mViewPager.setCurrentItem(position, false);

            } else if (position == adapter.getCount() - 1) {
                position = 1;
                mViewPager.setCurrentItem(1, false);
            }
            footer_shuffing.setText(((List<LearnHP.DataBean.CategoryBean.ItemBean>) mViewPager.getTag()).get(position - 1).getTitle());

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
